/*
 * Created on 2006-4-18 10:41:27
 */
package cn.com.sbell.vss.csg.vsclient.hikvision;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.sbell.vss.csg.vsclient.hikvision.bean.AlarmInCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.AlarmOutCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.AlarmOutState;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.CompressionCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.CompressionCfgEx;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.CruiseCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.CruiseCountCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.DecoderCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.DeviceCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.DeviceInfo;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.ExceptionCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.FrontIdCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.FtpUpgradeCFG;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.HideImageAreaCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.ImageFormat;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.JPEGCFG;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.ManageCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.NetCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.PPPoEConf;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.PTZResetCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.PicCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.RS232Cfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.RecordCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.RecordStatus;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.ResourceUsedInfo;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.SerialMessageInfo;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.TimeCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.UploadPic;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.UserCfg;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.VsCommonInfo;
import cn.com.sbell.vss.csg.vsclient.hikvision.bean.WorkState;
import cn.com.sbell.vss.csg.vsclient.util.CSGConfiguration;

/**
 * @author liang.a.chen
 */
public class HcNetAdapter {
	
    private static Log logger = LogFactory.getLog(HcNetAdapter.class);
    
    private static boolean isLoaded = false;
    static{
        try{
            System.loadLibrary("HcNetAdapter");
            isLoaded = true;
        }catch(Throwable t){
        	logger.warn(t.getMessage());
        }
    }
    
    public static void initialize(){
        if(isLoaded){
        	logger.info("Starting up hikvision alarm listener ...");
        	startupAlarmListen(CSGConfiguration.COMMON_ALARM_REPORT_IPADDRESS, CSGConfiguration.HIKVISION_ALARM_REPORT_PORT);
        	logger.info("Started up hikvision alarm listener successfully!");
        }
    }
    
    public static void destroy(){
        if(isLoaded){
        	logger.info("Stopping hikvision alarm listener ...");
        	//Conflict with mina
        	stopAlarmListen();
        	logger.info("Stopped hikvision alarm listener successfully!");
        }
    }
    
    public static native boolean startupAlarmListen(String cspIp, int csgPort);
    public static native boolean stopAlarmListen();
    public static native int login(String vsIp, int port, String username, String password, DeviceInfo deviceInfo);
    public static native boolean logout(int userId);
    public static native int getLastStatusCode();
    
    public static native boolean controlPtz(int userId, int channel, int ptzCmd, int isStop);
    public static native boolean controlPtzWithSpeed(int userId, int channel, int ptzCmd, int isStop, int speed);
    public static native boolean ptzPreset(int userId, int channel, int presetCmd, int presetIndex);
    public static native boolean reboot(int userId);
    public static native boolean shutdown(int userId);
    
    //Configure DVR
    public static native boolean setAlarmInCfg(int userId, int channel, AlarmInCfg alarmInCfg);
    public static native boolean getAlarmInCfg(int userId, int channel, AlarmInCfg alarmInCfg);
    
    public static native boolean setAlarmOutCfg(int userId, int channel, AlarmOutCfg alarmOutCfg);
    public static native boolean getAlarmOutCfg(int userId, int channel, AlarmOutCfg alarmOutCfg);
    
    public static native boolean setCompressionCfg(int userId, int channel, CompressionCfg compressionCfg);
    public static native boolean getCompressionCfg(int userId, int channel, CompressionCfg compressionCfg);
    
    public static native boolean setCompressionCfgEx(int userId, int channel, CompressionCfgEx compressionCfg);
    public static native boolean getCompressionCfgEx(int userId, int channel, CompressionCfgEx compressionCfg);
    
    public static native boolean setDecoderCfg(int userId, int channel, DecoderCfg decorderCfg);
    public static native boolean getDecoderCfg(int userId, int channel, DecoderCfg decorderCfg);
    
//	public static native boolean setFormatCfg(int userId, int channel, FormatCfg fmtCfg);
//	public static native boolean getFormatCfg(int userId, int channel, FormatCfg fmtCfg);
    
    public static native boolean setNetCfg(int userId, int channel, NetCfg netCfg);
    public static native boolean getNetCfg(int userId, int channel, NetCfg netCfg);
    
    public static native boolean setPicCfg(int userId, int channel, PicCfg picCfg);
    public static native boolean getPicCfg(int userId, int channel, PicCfg picCfg);
        
    public static native boolean setRS232Cfg(int userId, int channel, RS232Cfg rs232Cfg);
    public static native boolean getRS232Cfg(int userId, int channel, RS232Cfg rs232Cfg);
    
    public static native boolean setTimeCfg(int userId, int channel, TimeCfg timeCfg);
    public static native boolean getTimeCfg(int userId, int channel, TimeCfg timeCfg);
    
    public static native boolean setUserCfg(int userId, int channel, UserCfg userCfg);
    public static native boolean getUserCfg(int userId, int channel, UserCfg userCfg);
    
    public static native boolean setDeviceCfg(int userId, int channel, DeviceCfg deviceCfg);
    public static native boolean getDeviceCfg(int userId, int channel, DeviceCfg deviceCfg);
    
    public static native boolean setRecordCfg(int userId, int channel, RecordCfg recordCfg);
    public static native boolean getRecordCfg(int userId, int channel, RecordCfg recordCfg);
    
    public static native boolean setExceptionCfg(int userId, int channel, ExceptionCfg exptionCfg);
    public static native boolean getExceptionCfg(int userId, int channel, ExceptionCfg exptionCfg);
    
    public static native boolean getWorkState(int userId, WorkState workState);
    public static native boolean getStorageFiles(int userId, int channel, TimeCfg startTime, TimeCfg stopTime, int fileType, List fileList);
    
    public static native boolean setAlarmOut(int userId, int channel, int state);
    public static native boolean getAlarmOut(int userId, AlarmOutState alarmOutState);
    
    public static native boolean setManageCfg(int userId, ManageCfg manageCfg);
    public static native boolean getManageCfg(int userId, ManageCfg manageCfg);
    
    public static native boolean setFrontIdCfg(int userId, FrontIdCfg frontIdCfg);
    public static native boolean getFrontIdCfg(int userId, FrontIdCfg frontIdCfg);    


    public static native boolean getJPEGCFG(int userId,JPEGCFG jpegCfg);
    public static native boolean setJPEGCFG(int userId,JPEGCFG jpegCfg);
    

    public static native boolean getCapturePictureStorageFiles(int userId,int channel,int fileType,boolean needCardNum, byte[] cardNumber,TimeCfg startTime, TimeCfg stopTime,List fileList);
    
    //REMOTE UPGRADE
    public static native int upgrade(int userId, FtpUpgradeCFG upgradeCFG);
    public static native boolean closeUpgradeHandle(int handle);
    public static native int getUpgradeState(int handle);
    

    public static native boolean getCruiseConfig(int userId,int channel, int cruiseNo, CruiseCfg cruiseCfg);
    public static native boolean setCruiseConfig(int userId,int channel, int cruiseNo, CruiseCfg cruiseCfg);
    
    public static native boolean getCrusieCountCfg(int userId, int channel, CruiseCountCfg cruiseCntCfg);    

    protected static native boolean NET_DVR_PTZCruise_Other(int userId, int channel, int cruiseCmd, int cruiseRoute, int cruisePoint, int wInput);

    public static boolean startPTZCruise(int userId, int channel, int cruiseRoute) {
        return NET_DVR_PTZCruise_Other(userId, channel, RUN_SEQ, cruiseRoute, 0, 0);
    }
    
    public static boolean stopPTZCruise(int userId, int channel, int cruiseRoute) {
        return NET_DVR_PTZCruise_Other(userId, channel, STOP_SEQ, cruiseRoute, 0, 0);
    }
    

    public static native boolean sendSerialMessage(int userId, SerialMessageInfo serialMessageInfo);
    // constants
    public static final int RUN_SEQ = 37; 
    public static final int STOP_SEQ = 38; 
    

    public static native boolean setHideImageAreaCfg(int userId, int channel, HideImageAreaCfg hideImageAreaCfg);
    public static native boolean getHideImageAreaCfg(int userId, int channel, HideImageAreaCfg hideImageAreaCfg);
    public static native boolean getVsCommonInf(int userId, int channel,VsCommonInfo vsCommonInfo );
    public static native boolean setVsCommonInf(int userId, int channel,VsCommonInfo vsCommonInfo );
    public static native boolean setImageFormat(int userId, int channel,ImageFormat imageFormat);
    public static native boolean getImageFormat(int userId, int channel,ImageFormat imageFormat);

	public static native boolean getResourceUsedInfo(int userId,int channel,ResourceUsedInfo resourceUsedInfo);
	
	//PPPoE configuration
	public static native boolean getPPPoEPeriod(int userId,PPPoEConf conf);
	public static native boolean setPPPoEPeriod(int userId,PPPoEConf conf);
	//configure hik print log
	public static native boolean SetVsLogPrint(int isPrint);
	public static native boolean startRecord(int userId,RecordStatus rs);
	
//	upload picture
	public static native boolean setUploadPicture(int userId,UploadPic up);
	public static native boolean getUploadPicture(int userId,UploadPic up);
	
	//configure hik ptz preset
	public static native boolean setPTZPreset(int userId,PTZResetCfg reset,int ptzId);
	public static native boolean getPTZPreset(int userId,PTZResetCfg reset,int ptzId);
	
    public static native boolean setCompressionCfgEx_3N(int userId, int channel, CompressionCfgEx compressionCfg);
    public static native boolean getCompressionCfgEx_3N(int userId, int channel, CompressionCfgEx compressionCfg);

}
